﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Dto.Customer;
using CRM.Admin.Services.Service.Customer;
using KingsSharp.Common;
using KingsSharp.Common.Extensions;
using KingsSharp.Web.UI;
using KingsSharp.Core.Data.Entity;

namespace CRM.Admin.Areas.Customer.Controllers
{
    public class PurchaseController : Controller
    {
        public IPurchaseService PurchaseService { get; set; }
        public ActionResult PurchaseAdd(Guid CustomerId)
        {
            ViewBag.CustomerId = CustomerId;
            return View();
        }
        public ActionResult PurchaseEdit(Guid PurchaseId,Guid CustomerId)
        {
            var dto = PurchaseService.Purchases.FirstOrDefault(a=>a.Id==PurchaseId).MapTo<PurchaseDto>();
            ViewBag.CustomerId = CustomerId;
            return View(dto);
        }
        public ActionResult PurchaseInsert(PurchaseDto purchaseDto)
        {
            purchaseDto.CheckNotNull("purchaseDto");

            var result = PurchaseService.Insert(purchaseDto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "品种 “{0}” 的采购渠道添加成功。".FormatWith(purchaseDto.BreedName);
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult PurchaseUpdate(PurchaseDto dto)
        {
            dto.CheckNotNull("dto");
            var result = PurchaseService.Update(dto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "品种“{0}”的采购渠道更新成功。".FormatWith(dto.BreedName);
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult PurchaseDelete(Guid UserId)
        {
            UserId.CheckNotEmpty("UserId");
            Guid[] id = new Guid[] { UserId};
            var result = PurchaseService.Delete(id);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "采购渠道删除成功。";
            }
            return Json(new AjaxResult(msg, msgType));
        }
    }
}